﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Core.Base;
using System.ComponentModel;
using Base;
using System.Drawing;
using Core.Component;
using Core;

namespace Components
{
    [System.ComponentModel.DefaultBindingProperty("DateTimeValue")]
    public class SGVDateTimePicker : DateTimePicker, IBindingControl, ISGVControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        String strInitCustomFormat = "dd/MM/yyyy";
        String strEmptyFormat = " ";
        public SGVDateTimePicker()
            :base()
        {
            this.GotFocus += new EventHandler(Control_GotFocus);
            this.LostFocus += new EventHandler(Control_LostFocus);
            this.ValueChanged += new EventHandler(SGVDateTimePicker_ValueChanged);
        }

        public Core.Component.UIComponent component
        {
            set;
            get;
        }

        void SGVDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            DateTimeValue = this.Value;
        }

        public Nullable<DateTime> DateTimeValue
        {
            set
            {
                if (value == null || value == this.MinDate)
                {
                    this.Value = this.MinDate;
                    this.CustomFormat = strEmptyFormat;
                }
                else
                {
                    this.Value = value.Value;
                    this.CustomFormat = strInitCustomFormat;
                }

                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DateTimeValue"));
            }

            get
            {
                if (this.Value == this.MinDate)
                    return null;

                return this.Value;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.KeyCode == Keys.Delete)
                this.DateTimeValue = this.MinDate; 
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        void Control_LostFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlLostFocus(this);
        }

        void Control_GotFocus(object sender, EventArgs e)
        {
            SGVControlHelper.ControlFocus(this);
            //this.CalendarMonthBackground = Color.Yellow;
        }


        #region IBindingControl
        BaseModule module;
        BaseEntity entity;
        protected String _bindingPropertyName = "DateTimeValue";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;

        String _sGVShortcutString;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String SGVShortcutString
        {
            get { return _sGVShortcutString; }
            set { _sGVShortcutString = value; }
        }
        String _sGVTag;
        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string SGVTag
        {
            get
            {
                return _sGVTag;
            }
            set
            {
                _sGVTag = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }

        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        #endregion

        public void Init()
        {
            _bindingPropertyName = "DateTimeValue";

            if (String.IsNullOrEmpty(AppContext.CurrentCompanyInfo.DateFormat) == false)
                strInitCustomFormat = AppContext.CurrentCompanyInfo.DateFormat;
            
            this.Format = DateTimePickerFormat.Custom;
            this.CustomFormat = strInitCustomFormat;
            
        }

        public void UpdateFormat()
        {
        }




        public UIComponent Serialize()
        {
            UIComponent component = new UIComponent();
            SGVControlHelper.CreateUIComponentFromControl(component, this);

            if (component != null)
            {
                component.Value = this.Value;
            }
            return component;
        }

        public void Deserialize(UIComponent component)
        {
            throw new NotImplementedException();
        }
    }
}
